<?php

namespace backend\models;

use yii\base\Model;

class OrderForm extends Model {
	
	/**
	 * 定义属性
	 * @var unknown
	 */
	public $member;    		//会员名
	public $mail;      		//邮箱
	public $recMan;    		//收货人
	public $post;      		//邮编
	public $tel;      		//手机号
	public $address;   		//收货地址
	public $delivery;  		//物流
	public $state;     		//订单状态
	public $commodity;  	//商品名
	public $num;			//数量
	public $unit;			//单位
	public $price;			//单价
	public $totalPrice;		//总价
	
	
	/**
	 * 定制规则
	 * @see \yii\base\Model::rules()
	 */
	public function rules(){
		
		return [
						
			//修改规则
			[['recMan','post','tel','address','num','price'],'required','message'=>'{attribute}不能为空','on'=>'alter'],
			['recMan','string','on'=>'alter','max'=>10,'tooLong'=>'收货人超过10个字符'],
			['post','string','on'=>'alter','max'=>10,'tooLong'=>'邮编超过10个字符'],
			['tel','integer','on'=>'alter','message'=>'电话号码必须为数字',],
			['num','validateNum','on'=>'alter'],
			['price','validatePrice','on'=>'alter'],

		
		];
		
	}
	
	
	/**
	 * 布置场景
	 * @see \yii\base\Model::scenarios()
	 */
	public function scenarios(){
		
		return [
			'view'=>['member','mail','recMan','post','tel','address','delivery','state','commodity','num','unit','price','totalPrice'],
			'alter'=>['member','mail','recMan','post','tel','address','delivery','state','commodity','num','unit','price','totalPrice']
		];
	}
	
	
	/**
	 * 属性标签
	 * @see \yii\base\Model::attributeLabels()
	 */
	public function attributeLabels()
	{
		return [
			'member' => '会员名',
			'mail' => '邮箱',
			'recMan' => '收货人',
			'post' => '邮编',
			'tel' => '手机号码',
			'address' => '收货地址',			
			'delivery' => '物流',
			'state' => '订单状态',
			'commodity' => '商品名',
			'num' => '数量',
			'unit' => '单位',
			'price' => '单价',
			'totalPrice' => '总价',
		];
	}
	
	
	/**
	 * 自定义规则
	 */
	
	public  function validateNum($attribute,$paramars){
		//如果没有错误则进行下面的验证
		if(!$this->hasErrors()){			
			if( !is_numeric($this->num))
				return $this->addError($attribute,'数量只能为数字类型');
			if($this->num == '' || $this->num == 0 )
				return $this->addError($attribute,'数量不能为空或0');
			
		}
	}
	
	public  function validatePrice($attribute,$paramars){
		//如果没有错误则进行下面的验证
		if(!$this->hasErrors()){
			if( !is_numeric($this->price))
				return $this->addError($attribute,'单价只能为数字类型');
			if($this->price == '' || $this->price == 0 )
				return $this->addError($attribute,'单价不能为空或0');
			
		}
	}
	
	
}

?>